//
//  RegisterViewController.swift
//  TestDemo
//
//  Created by 赵洋 on 2017/5/29.
//  Copyright © 2017年 赵洋. All rights reserved.
//

import UIKit
import MBProgressHUD
import Alamofire
import AVFoundation



internal class RegisterViewController: UITableViewController,UITextFieldDelegate {
    static let unwindSegue = "Register2Login"
    
    @IBOutlet private weak var userNameTF: UITextField!
    @IBOutlet private weak var passwordTF: UITextField!
    @IBOutlet weak var nameTF: UITextField!
    
    @IBOutlet weak var schoolIDTF: UITextField!
    
    @IBOutlet weak var sexControl: UISegmentedControl!
    
    @IBOutlet weak var emailTF: UITextField!
    
    @IBOutlet weak var phoneTF: UITextField!
    
    
    private(set) var userInfo:[String:String]?
    
    //MARK: super
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true);
    }
    //MARK: IBAction
    @IBAction private func registerAction() {
        view.endEditing(true)
        guard  let userName = userNameTF.text , userName != "" else {
            MBProgressHUD.showErrorWith(text: "请输入用户名！")
            return
        }
        guard let password = passwordTF.text , password != "" else {
            MBProgressHUD.showErrorWith(text: "请输入密码！")
            return
        }
        guard let name = nameTF.text ,name != "" else{
            MBProgressHUD.showErrorWith(text: "请输入姓名！")
            return
        }
        guard let schoolID = schoolIDTF.text ,schoolID != "" else{
            MBProgressHUD.showErrorWith(text: "请输入学号！")
            return
        }
       
        guard let email = emailTF.text ,email != "" else{
            MBProgressHUD.showErrorWith(text: "请输入邮箱！")
            return
        }
        guard let phone = phoneTF.text ,phone != "" else{
            MBProgressHUD.showErrorWith(text: "请输入手机号！")
            return
        }
        let sex = sexControl.selectedSegmentIndex == 0 ? "男":"女"
        
        userInfo = ["username":userName , "password":password , "name":name, "stu_number":schoolID,"email":email,"phone":phone,"sex":sex,"addnew":"1","device":"ios"]
        
        MBProgressHUD.showLoadingWith(Text: "请求中...")
        Alamofire.request("http://139.199.100.228/yonghuzhuce_add.php", method: .post, parameters: userInfo).responseJSON { [unowned self] response in
  
            MBProgressHUD.hideLoading()
            if let JSON = response.result.value ,let result = JSON as? [String:Int] ,let flag = result["flag"] {
                guard flag == 1 else {
                    if(flag == 0){
                        MBProgressHUD.showErrorWith(text: "保存失败")
                    }else{
                        MBProgressHUD.showErrorWith(text: "重复添加")
                    }
                    return
                }
                MBProgressHUD.showSuccessWith(text: "注册成功")
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2, execute: {
                    self.performSegue(withIdentifier: RegisterViewController.unwindSegue, sender: nil)
                })
                
               
            }else{
                MBProgressHUD.showErrorWith(text: "网络异常")
            }
        }
 
        //let url = URL(string: "http://139.199.100.228/yonghuzhuce_add.php")
        
        
        
        
        
        //performSegue(withIdentifier: RegisterViewController.unwindSegue, sender: nil)
    }
    
    //MARK: UITextFieldDelegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField == userNameTF {
            textField.resignFirstResponder()
            passwordTF.becomeFirstResponder()
        }else if textField == passwordTF{
            textField.resignFirstResponder()
            nameTF.becomeFirstResponder()
        }else if textField == nameTF{
            textField.resignFirstResponder()
            schoolIDTF.becomeFirstResponder()
        }else if  textField == schoolIDTF {
            textField.resignFirstResponder()
            emailTF.becomeFirstResponder()
        }else if  textField == emailTF {
            textField.resignFirstResponder()
            phoneTF.becomeFirstResponder()
        }else{
            phoneTF.resignFirstResponder()
            registerAction()
        }
        return true
    }

}
